package com.example.a2032.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.a2032.R;
import com.example.a2032.Utils.ContactUtils;
import com.example.a2032.Utils.Utils;
import com.example.a2032.Utils.WebSocketUtils;
import com.example.a2032.Utils.WifiConnectUtils;
import com.example.a2032.activity.BpMeasureActivity;
import com.example.a2032.activity.ContactListActivity;
import com.example.a2032.activity.EcgMeasureActivity;
import com.example.a2032.activity.HeartRateMeasureActivity;
import com.example.a2032.activity.SettingActivity;
import com.example.a2032.activity.Spo2MeasureActivity;
import com.example.a2032.activity.TelActivity;
import com.example.a2032.activity.TempMeasureActivity;

import java.security.spec.PSSParameterSpec;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link OpFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class OpFragment extends BaseFragment {
    public static final String KEY_Title = "Title";
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.iv_op)
    ImageView ivOp;
    @BindView(R.id.tv_op)
    TextView tvOp;

    @BindArray(R.array.item_opString)
    String[] opNames;

    // TODO: Rename and change types of parameters
    private int mParam1;
    private String mParam2;
    int position;

    public OpFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment OpFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static OpFragment newInstance(int position) {
        OpFragment fragment = new OpFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, position);
//        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            position = getArguments().getInt(ARG_PARAM1);
            // mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_op, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    String opName = "";

    private void init() {
        opName = opNames[position];
        tvOp.setText(opName);
        int[] resIds = Utils.getResourcesImageArray(getContext(), R.array.item_opImgId);
        ivOp.setImageResource(resIds[position]);

    }

    @OnClick(R.id.iv_op)
    public void onViewClicked(View view) {
      //  if(Utils.isFastClick(view))return;
        startActivityWithTrans(getOpActivity(), KEY_Title, opNames[position]);
    }

    Class getOpActivity() {
        Class activity = null;
        if (getString(R.string.op_setting).equals(opName)) {
            activity = SettingActivity.class;
        } else if (getString(R.string.op_chat).equals(opName)) {
            activity = ContactListActivity.class;
        } else if (getString(R.string.op_tel).equals(opName)) {
            //ContactUtils.getAllSmsInfo(getContext(),"2021.11.29 15:27:32 000");
            activity = TelActivity.class;
        } else if (getString(R.string.op_hr).equals(opName)) {
            WebSocketUtils.getInstance().setMeasuring(true);
            activity = HeartRateMeasureActivity.class;
        } else if (getString(R.string.op_spo2).equals(opName)) {
            WebSocketUtils.getInstance().setMeasuring(true);
            activity = Spo2MeasureActivity.class;
        } else if (getString(R.string.op_ecg).equals(opName)) {
            WebSocketUtils.getInstance().setMeasuring(true);
            activity = EcgMeasureActivity.class;
        } else if (getString(R.string.op_bp).equals(opName)) {
            WebSocketUtils.getInstance().setMeasuring(true);
            activity = BpMeasureActivity.class;
        } else if (getString(R.string.op_temp).equals(opName)) {
            WebSocketUtils.getInstance().setMeasuring(true);
            activity = TempMeasureActivity.class;
        }
        return activity;
    }
}